package unteractivities.p3_fitnessapp;

import model.p3_fitnessapp.History;
import model.p3_fitnessapp.MySQLiteHelper;
import model.p3_fitnessapp.Timemanager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import controler.p3_fitnessapp.MainActivity;
import controler.p3_fitnessapp.R;

/**
 * 
 * @author tcockx
 * 
 */
public class UActExercise extends Activity {
	
	
	//FIXME Uebung speichern 
	//FIXME Training beenden (Popup)

	/*
	 * ==========================================================================
	 * Globale Variablen
	 * ========================================================
	 */
     
     private int ex_Id= 0;
     private String ex_name;
	/*
	 * ==========================================================================
	 * Android Methoden ========================================================
	 */

	/**
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.uact_exercise);

	}

	@Override
	protected void onResume() {
		super.onResume();
		Intent intentEx = getIntent();
		ex_Id = intentEx.getIntExtra("ex_id", -1);
		ex_name = intentEx.getStringExtra("ex_name");
		
		System.out.println("Id :" + ex_Id);
		System.out.println("Name :" +ex_name);
		
		TextView TextExerciseName = (TextView)  findViewById(R.id.TextexerciseName);
		TextExerciseName.setText(ex_name);
	}
	
	
	/*
	 * ==========================================================================
	 * Benutzer Interaktion (Buttons ... )
	 * ========================================================
	 */
	
	public void onButtonClick(View view){
		
		
		/*
		 * Edit Text fuer Textfelder (Satz&Wiederholungen)
		 */
		EditText Textkg1 = (EditText) findViewById(R.id.Textkg1);
		EditText Textwdh2 = (EditText) findViewById(R.id.Textwdh2);
		EditText Textkg3 = (EditText) findViewById(R.id.Textkg3);
		EditText Textwdh4 = (EditText) findViewById(R.id.Textwdh4);
		EditText Textkg5 = (EditText) findViewById(R.id.Textkg5);
		EditText Textwdh6 = (EditText) findViewById(R.id.Textwdh6);
		MySQLiteHelper dbsatz = new MySQLiteHelper(getApplicationContext());
	    Timemanager time = new Timemanager();
		
		
		switch (view.getId()) {
		case R.id.btn_next_exercise:
//			startActivity(new Intent(this, UActTPChooseExercise.class));
			onBackPressed();
			
			
			// FIXME WTF ??? 6 Objekte ? 1 wuerde ausreichen
		
			History history1 = new History();
		
		    if ((Textkg1.getText().toString().length() > 0) && (Textwdh2.getText().toString().length() > 0)){
		    	history1.setSet(1);
		    	history1.setKilograms(Integer.parseInt(Textkg1.getText().toString()));
		    	history1.setRepetition(Integer.parseInt(Textwdh2.getText().toString()));
		    	history1.setDate(time.calendarToString(time.createCurrentCalendar()));
		    	history1.setId_exercise(ex_Id);
		    	dbsatz.addHistory(history1);
		    }
		    
			History history2 = new History();
			
			if((Textwdh4.getText().toString().length() > 0) && (Textkg3.getText().toString().length() > 0) ) {
				history2.setKilograms(Integer.parseInt(Textkg3.getText().toString()));
				history2.setRepetition(Integer.parseInt(Textwdh4.getText().toString()));
				history2.setSet(2);
				history2.setId_exercise(ex_Id);
				history2.setDate(time.calendarToString(time.createCurrentCalendar()));
				dbsatz.addHistory(history2);
			}
			
			History history3 = new History();
			
			if((Textwdh6.getText().toString().length() > 0) && (Textkg5.getText().toString().length() > 0) ){
				history3.setKilograms(Integer.parseInt(Textkg5.getText().toString()));
				history3.setRepetition(Integer.parseInt(Textwdh6.getText().toString()));
				history3.setSet(3);
				history3.setId_exercise(ex_Id);
				history3.setDate(time.calendarToString(time.createCurrentCalendar()));
				dbsatz.addHistory(history3);
			
			}
		
			break;
			
		case R.id.btn_stop_training:
			
			History history4 = new History();
			
		    if ((Textkg1.getText().toString().length() > 0) && (Textwdh2.getText().toString().length() > 0)){
		    	history4.setSet(1);
		    	history4.setKilograms(Integer.parseInt(Textkg1.getText().toString()));
		    	history4.setRepetition(Integer.parseInt(Textwdh2.getText().toString()));
		    	history4.setDate(time.calendarToString(time.createCurrentCalendar()));
		    	history4.setId_exercise(ex_Id);
		    	dbsatz.addHistory(history4);
		    }
		    
			History history5 = new History();
			
			if((Textwdh4.getText().toString().length() > 0) && (Textkg3.getText().toString().length() > 0) ) {
				history5.setKilograms(Integer.parseInt(Textkg3.getText().toString()));
				history5.setRepetition(Integer.parseInt(Textwdh4.getText().toString()));
				history5.setSet(2);
				history5.setId_exercise(ex_Id);
				history5.setDate(time.calendarToString(time.createCurrentCalendar()));
				dbsatz.addHistory(history5);
			}
			
			History history6 = new History();
			
			if((Textwdh6.getText().toString().length() > 0) && (Textkg5.getText().toString().length() > 0) ){
				history6.setKilograms(Integer.parseInt(Textkg5.getText().toString()));
				history6.setRepetition(Integer.parseInt(Textwdh6.getText().toString()));
				history6.setSet(3);
				history6.setId_exercise(ex_Id);
				history6.setDate(time.calendarToString(time.createCurrentCalendar()));
				dbsatz.addHistory(history6);
			
			}
			
			startActivity(new Intent(this, MainActivity.class));

			break;
		default:
			break;
		}
	
	}
	/*
	 * ==========================================================================
	 * Eigene Methoden ========================================================
	 */

	/*
	 * ==========================================================================
	 * Debug Methoden ========================================================
	 */

	/*
	 * ==========================================================================
	 * Getter und Setter
	 * ========================================================
	 */

}
